package cn.lyjuan.pojoutils.genclass;

import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.pojoutils.bean.ColumnBean;
import cn.lyjuan.pojoutils.bean.TableBean;
import cn.lyjuan.pojoutils.cst.BaseCst;
import cn.lyjuan.pojoutils.util.PojoUtils;

import java.util.List;

/**
 * Created by ly on 2014/11/21.
 */
public class AGenClass implements IGenClass
{
    @Override
    public String gen(TableBean tableBean, List<ColumnBean> cols)
    {
        StringBuilder sb = new StringBuilder();

        sb.append(genPkg(tableBean)).append("\n\n")// 包名
        .append(genImport(cols)).append("\n\n")// 导入类型
        .append(genClass(tableBean)).append("\n")// 类
        .append(genMembers(tableBean, cols)).append("\n")// 属性
        .append(genGtterAndSetter(cols)).append("\n\n")// Getter Setter 方法
        .append(getNoneParamsConstructor(tableBean)).append("\n\n")// 无参构造器
        .append(genToString(tableBean, cols)).append("\n")// toString 方法
        .append("}");

        return sb.toString();
    }

    @Override
    public String genImport(List<ColumnBean> cols)
    {
        if (null == cols || cols.isEmpty()) return "";
        // 目前只有 Date
        for (ColumnBean c : cols)
        {
            if ("date" .equals(c.typeName.toLowerCase()))
                return "import java.util.Date;";
        }

        return "";
    }

    @Override
    public String genPkg(TableBean tableBean)
    {
        return "package " + tableBean.getPojoPkgName() + ";";
    }

    @Override
    public String genClass(TableBean tableBean)
    {
        return "/**\n * " + (null == tableBean.getPojoComment() ? "" : tableBean.getPojoComment())
                + "\n * " + BaseCst.CREATE_INFO
                + "\n */\npublic class " + tableBean.getPojoName() + "\n{"
                // 表名 TABLE_NAME
                + "\n\t/**\n\t 表名\n\t */"
                + "\n\tpublic transient static final String TABLE_NAME = \""
                + tableBean.getTableName().toUpperCase() + "\";\n";
    }

    @Override
    public String genMembers(TableBean tableBean, List<ColumnBean> cols)
    {
        if (null == cols || cols.isEmpty())
            return "";

        StringBuilder sb = new StringBuilder();

        for (ColumnBean c : cols)
        {
            sb.append("\t/**");
            // 主键信息
            sb.append("\n\t * ");
            if (c.isPriKey) sb.append("主键，");
            if (c.autoInc) sb.append("自动增长");
            else if (!StringUtils.isNull(c.seq)) sb.append("使用序列 ").append(c.seq);
            // 注释
            if (!StringUtils.isNull(c.comment))
                sb.append("\n\t * ").append(c.comment);
            // 数据库类型
            sb.append("\n\t * 数据库类型：").append(c.type).append(":").append(c.dbTypeName)
                    .append("timestamp" .equals(c.dbTypeName.toLowerCase()) ? "MySQL 中 timestamp 每次更新都会更新为当前时间" : "")
                    .append("\n");
            // 默认值
            if (!StringUtils.isNull(c.defVal)) sb.append("\t * 默认值：").append(c.defVal).append("\n");
            // 是否可为空
            sb.append("\t * ").append(c.isNull ? "可为空" : "不可为空");
            sb.append("\n\t */\n\t");

            sb.append("private ").append(PojoUtils.typeCaseForClass(c.typeName.toLowerCase())).append(" ").append(c.column);

            if (PojoUtils.isTypeNum(c.typeName))
                sb.append(" = ").append(tableBean.getNumberInvalidVal());

            sb.append(";\n\n");
        }
        return sb.toString();
    }

    @Override
    public String genGtterAndSetter(List<ColumnBean> cols)
    {
        StringBuilder sb = new StringBuilder();

        sb.append("\t/** Getter and Setter Methods **/\n");
        for (ColumnBean c : cols)
        {
            sb.append("\tpublic ").append(PojoUtils.typeCaseForClass(c.typeName.toLowerCase()))
            .append(" get").append(PojoUtils.firstUpLowCase(c.column, true))
            .append("() { return this.").append(c.column).append("; }\n")
            .append("\tpublic void set").append(PojoUtils.firstUpLowCase(c.column, true))
            .append("(").append(PojoUtils.typeCaseForClass(c.typeName)).append(" ").append(c.column)
            .append(") {this.").append(c.column).append(" = ").append(c.column).append("; }\n");
        }
        return sb.toString();
    }

    @Override
    public String genToString(TableBean tableBean, List<ColumnBean> cols)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("\tpublic String toString()\n\t{\n\t\treturn \"" + tableBean.getPojoName() + "[");
        for (ColumnBean c : cols)
        {
            sb.append(c.column).append(" = \" + this.").append(c.column).append(" + \", \" + \n\t\t\"");
        }
        if (cols.size() > 0)
            sb.delete(sb.length() - 13, sb.length());

        sb.append(" + \"]\";\n\t}");

        return sb.toString();
    }

    @Override
    public String getNoneParamsConstructor(TableBean tableBean)
    {
        return "\tpublic " + tableBean.getPojoName() + "(){}\n";
    }
}





















